﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Emerson.Storage.Service.Interface;
using Emerson.Storage.Entities;
using System.Web.Services;
using System.Web.Script.Services;

namespace WorkstationDashboard
{
    public partial class ContentWorkstation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BuildTreeView();
                workstationId.Value = Request.Params["id"];
                Utility.SelectNode(Master.WorkstationTreeView, NodeType.Workstation, int.Parse(workstationId.Value));
                SetWorkstationInfo();
            }
        }

        private void SetWorkstationInfo()
        {
            int id = int.Parse(workstationId.Value);
            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            Workstation workstation = wsService.GetWorkstation(id);
            workstationName.Text = workstation.Name;
            workstationName2.Text = workstation.Name;
            workstationIP.Text = workstation.IPAddress;
            workstationNote.Text = workstation.Note;
            osVersion.Text = workstation.OSVersion;
        }

        private void BuildTreeView()
        {
            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();

            WorkstationRoot wkRoot = wsService.GetWorkstationRoot();
            TreeNode treeRoot = Utility.BuildTreeFromWorkstationRoot(wkRoot);
            Master.WorkstationTreeView.Nodes.Clear();
            Master.WorkstationTreeView.Nodes.Add(treeRoot);
        }

        [WebMethod]
        [ScriptMethod]
        public static void RemoveWorkstation(string idString)
        {
            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            int workstationId = int.Parse(idString);
            wsService.DeleteWorkstation(workstationId);
        }
    }
}
